﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core;
using Core.Factory;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NLog;
using WebApp.Models;

namespace WebApp.ApiControllers
{
    /// <summary>
    /// 积分管理
    /// </summary>
    [Route("api/[controller]/[action]")]

    public class PointsController : Controller
    {
        private readonly IBaseService _baseService;
        private readonly Logger log = LogManager.GetCurrentClassLogger();
        private readonly IUserService _userService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseService"></param>
        /// <param name="userService"></param>
        public PointsController(IBaseService baseService, IUserService userService)
        {
            _baseService = baseService;
            _userService = userService;
        }
        /// <summary>
        /// AddPoints
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel AddPoints(T_PointsDetail detail)
        {
            ResultModel result = new ResultModel();
            try
            {
                _userService.AddPoints(detail);
                result.result = (int)MsgType.success;
                result.type = MsgType.success.ToString();
                result.msg = "积分添加成功";
                result.code = "200";
            }
            catch (Exception ex)
            {
                log.Error(ex, ex.Message);
                result.msg = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// GetPoints
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel GetPoints(int userId, int pageIndex = 1, int pageSize = 20)
        {
            ResultModel result = new ResultModel();
            try
            {
                int count = 0;
                var list = _baseService.GetList<T_PointsDetail>(pageIndex, pageSize, ref count, i => i.UserId == userId, i => i.Desc(o => o.CreateDate));
                result.result = (int)MsgType.success;
                result.data = list;
                result.type = MsgType.success.ToString();
                result.code = "200";
            }
            catch (Exception ex)
            {
                log.Error(ex, ex.Message);
                result.msg = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 获取规则
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultModel GetPointsRule()
        {
            ResultModel result = new ResultModel();
            try
            {
                var list = _baseService.GetList<T_PointsRule>(i=>true);
                result.result = (int)MsgType.success;
                result.data = list;
                result.type = MsgType.success.ToString();
                result.code = "200";
            }
            catch (Exception ex)
            {
                log.Error(ex, ex.Message);
                result.msg = ex.Message;
            }
            return result;
        }
       /// <summary>
       /// 获取用户积分
       /// </summary>
       /// <param name="userId"></param>
       /// <returns></returns>
        [HttpGet]
        public ResultModel GetUserPoints(int userId)
        {
            ResultModel result = new ResultModel();
            try
            {
                var model = _baseService.GetModel<T_User>(i => i.Id==userId);
                result.result = (int)MsgType.success;
                result.data = model.Points;
                result.type = MsgType.success.ToString();
                result.code = "200";
            }
            catch (Exception ex)
            {
                log.Error(ex, ex.Message);
                result.msg = ex.Message;
            }
            return result;
        }
    }
}